package b3.widget.guinness
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.basic.*;
	import b3.widget.tab.*;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filters.*;
	import flash.text.*;
	
	public class GuinnessDialog extends TabController
	{
		private static var instance:GuinnessDialog;
		private var guinness:Guinness;
		
		public function GuinnessDialog()
		{
			var GuinnessDialogBgr:Class = ResourceHelper.getInstance().getClassByName("GuinnessDialogBgr");
			var GuinnessTab:Class = ResourceHelper.getInstance().getClassByName("GuinnessTab1");
			var GuinnessTabHighlight:Class = ResourceHelper.getInstance().getClassByName("GuinnessTab2");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButtonGuinness");
			var TabPageFirstPage:Class = ResourceHelper.getInstance().getClassByName("TabPageFirstPage4");
			var TabPagePageUp:Class = ResourceHelper.getInstance().getClassByName("TabPagePageUp4");
			var TabPagePageDown:Class = ResourceHelper.getInstance().getClassByName("TabPagePageDown4");
			var TabPageLastPage:Class = ResourceHelper.getInstance().getClassByName("TabPageLastPage4");
			super(GuinnessDialogBgr, CloseButton);
			this.anchor = BasicWidget.ANCHOR_MIDDLE_MIDDLE;
			//第一块
			var tab:Tab = new Tab("最高\n  建筑\n ", 2, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xC4E6EE, 0x015C49);
			tab.setPageSelectorPos(257, 385);
			this.addTab(GuinnessTab, tab.tabName, tab, GuinnessTabHighlight);
			tab = new Tab("最高\n  导弹\n ", 2, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xC4E6EE, 0x015C49);
			tab.setPageSelectorPos(257, 385);
			this.addTab(GuinnessTab, tab.tabName, tab, GuinnessTabHighlight);
			tab = new Tab("最高\n  绿化\n ", 2, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xC4E6EE, 0x015C49);
			tab.setPageSelectorPos(257, 385);
			this.addTab(GuinnessTab, tab.tabName, tab, GuinnessTabHighlight);
			tab = new Tab("最高\n  奇迹\n ", 2, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xC4E6EE, 0x015C49);
			tab.setPageSelectorPos(257, 385);
			this.addTab(GuinnessTab, tab.tabName, tab, GuinnessTabHighlight);
			this.setTabTitlePos(34, 7, 80); 
			this.setTabPagePos(98, 120);
			this.setCloseButtonPos(320, 530);
			//第二块
			tab = new Tab("最高\n  街区\n ", 2, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xC4E6EE, 0x015C49);
			tab.setPageSelectorPos(257, 385);
			this.addTab(GuinnessTab, tab.tabName, tab, GuinnessTabHighlight);
			this.tabTitles[4].x = 420;
			this.tabTitles[4].y = 7;
			tab = new Tab("最多\n  城市\n ", 2, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xC4E6EE, 0x015C49);
			tab.setPageSelectorPos(257, 385);
			this.addTab(GuinnessTab, tab.tabName, tab, GuinnessTabHighlight);
			this.tabTitles[5].x = 500;
			this.tabTitles[5].y = 7;
			tab = new Tab("最多\n  次数\n ", 2, 2);
			tab.setPageSelectorStyle(TabPageFirstPage, TabPagePageUp, TabPagePageDown, TabPageLastPage, 0xC4E6EE, 0x015C49);
			tab.setPageSelectorPos(257, 385);
			this.addTab(GuinnessTab, tab.tabName, tab, GuinnessTabHighlight);
			this.tabTitles[6].x = 580;
			this.tabTitles[6].y = 7;
			
			var GuinnessDialogBgr2:Class = ResourceHelper.getInstance().getClassByName("GuinnessDialogBgr2");
			var guinnessDialogBgr2:Sprite = new GuinnessDialogBgr2();
			guinnessDialogBgr2.x = 27;
			guinnessDialogBgr2.y = 40;
			guinnessDialogBgr2.mouseChildren = false;
			guinnessDialogBgr2.mouseEnabled = false;
			this.addChildAt(guinnessDialogBgr2, 2);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			this.guinness = new Guinness();
		}
		
		public static function getInstance():GuinnessDialog
		{
			if (!instance)
				instance = new GuinnessDialog();
			return instance;
		}
		
		private function onAddedToStage(event:Event):void
		{
			//随机选择页面（平均曝光各项吉尼斯）
			var randomTabIndex:int = this.tabs.length * Math.random();
			this.selectTab(randomTabIndex);
			//获得世界纪录
			guinness.getWorldRecords(true);
			guinness.addEventListener(ModelEvent.GET_WORLD_RECORDS, onRefreshWorldRecords);
		}

		private function onRemovedFromStage(event:Event):void
		{
			guinness.removeEventListener(ModelEvent.GET_WORLD_RECORDS, onRefreshWorldRecords);
		}
		
		private function onRefreshWorldRecords(event:Event):void
		{
			removeAllItems();
			//本人是否保持世界纪录
			var holdWorldRecord:Boolean = false;
			for (var recordIndexStr:String in guinness.worldRecords)
			{
				var worldRecord:RecordInfo = guinness.worldRecords[recordIndexStr];
				var personalRecord:RecordInfo = guinness.personalRecords[recordIndexStr];
				var guinnessItem:GuinnessItem = new GuinnessItem(worldRecord, personalRecord);
				var tab:Tab = tabs[worldRecord.type];
				tab.addItem(guinnessItem, 312, 206);
				if (worldRecord != null && personalRecord != null &&
					worldRecord.holderId == personalRecord.holderId)
				{
					holdWorldRecord = true;
				}
			}
			if (holdWorldRecord)
			{
				var me:Player = PlayerManager.getInstance().me;
				me.taskList.finishTask(TaskConfig.ID_ATTEND_GUINNESS);
			}
		}
		
	}
}